﻿using System;
using System.IO;

namespace ConsoleApp3
{
    class Program
    {
       static void WriterRecord(string path)
        {
            Console.WriteLine("请输入：");
            string record = Console.ReadLine();
            //创建文件流
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
            //创建流写入对象
            StreamWriter sw = new StreamWriter(fs);
            //执行方法，将内容写入文件
            sw.Write(record);
            //关闭文件流写入对象
            sw.Close();
            //关闭文件流
            fs.Close();
            Console.WriteLine("写入成功");
        }

         //读取
         static void ReadRecord(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
            //创建流读取对象
            StreamReader sd = new StreamReader(fs);
            //执行方法，读取文本文件数据
            string record = sd.ReadToEnd();
            //关闭流读取对象
            sd.Close();
            //关闭文件流
            fs.Close();
            Console.WriteLine("读取成功，如下：\n"+record);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("请选择");
            Console.WriteLine("1.读取数据  2.写入数据");
            string f = Console.ReadLine();
            if (f == "1")
            {
                ReadRecord("d:\\text.txt");
            }else if(f == "2")
                {
                    WriterRecord("d:\\test.txt");
                }
        }
    }
}
